package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectDanger;
import com.mxpio.erp.common.plm.entity.ProjectInfo;
import com.mxpio.erp.common.plm.service.ProjectDangerService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectDangerController", description = "项目-风险列表")
@RestController
@RequestMapping("/erp/plm/pro/danger/")
public class ProjectDangerController {

    @Autowired
    private ProjectDangerService projectDangerService;

    @GetMapping("list")
    @Operation(summary = "项目风险列表", description = "项目风险列表（无分页）", method = "GET")
    public Result<List<ProjectDanger>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectDanger> list = projectDangerService.list(ProjectDanger.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "项目风险列表", description = "项目风险列表（分页）", method = "GET")
    public Result<Page<ProjectDanger>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectDanger> pageResult = projectDangerService.listPage(ProjectDanger.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据项目编码获取风险列表", description = "根据项目编码获取风险列表（无分页）", method = "GET")
    public Result<List<ProjectDanger>> listDangger(@Parameter(description="查询参数") String proCode) throws UnsupportedEncodingException {
        Criteria criteria = Criteria.create().addCriterion("proCode", Operator.EQ, proCode);
        List<ProjectDanger> list = projectDangerService.list(ProjectDanger.class, criteria);
        return Result.OK(list);
    }


    @GetMapping("list/{dangerCode}")
    @Operation(summary = "根据风险编码获取风险列表", description = "根据风险编码获取风险列表", method = "GET")
    public Result<ProjectDanger> getById(@PathVariable(name = "dangerCode", required = true) String dangerCode) {
        ProjectDanger projectDanger = projectDangerService.getById(ProjectDanger.class, dangerCode);
        return Result.OK(projectDanger);
    }

    @RequestMapping("add")
    @Operation(summary = "新增风险信息", description = "新增风险信息")
    public Result<ProjectDanger> add(@RequestBody ProjectDanger projectDanger) {
        projectDangerService.save(projectDanger);
        return Result.OK(projectDanger);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑风险信息", description = "编辑风险信息", method = "PUT")
    public Result<ProjectDanger> edit(@RequestBody ProjectDanger projectDanger) {
        projectDangerService.update(projectDanger);
        return Result.OK(projectDanger);
    }

    @DeleteMapping(value = "/delete/{dangerCode}")
    @Operation(summary = "删除危险信息",description = "删除危险信息", method = "DELETE")
    public Result<?> delete(@PathVariable(name = "dangerCode") String dangerCode) {
        projectDangerService.delete(dangerCode,ProjectDanger.class);
        return Result.OK("删除成功");
    }

    @RequestMapping("audit")
    @Operation(summary = "审核风险信息", description = "审核风险信息", method = "POST")
    public Result<ProjectDanger> audit(@RequestBody ProjectDanger projectDanger) {
        projectDangerService.audit(projectDanger);
        return Result.OK(projectDanger);
    }


}
